package com.third.customer.site.controller.resp;

import com.third.customer.model.data.UserType;
import com.third.customer.model.tables.User;
import com.third.customer.service.utils.TimeUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * Created by hehuaichun on 2020/5/24.
 */
@Data
@Slf4j
public class UserResp {
    private String id;
    private UserType type;
    private String name;
    private String updatedAt;
    private String createdAt;

    public static UserResp convert(User user) {
        UserResp resp = new UserResp();
        resp.setId(String.valueOf(user.getId()));
        resp.setName(user.getName());
        try {
            resp.setType(UserType.codeOf(user.getType()));
        } catch (Exception e) {
            log.error("invalid user type code [{}],user id:[{}]", user.getType(), user.getId());
        }
        if (user.getCreatedAt() != null) {
            resp.setCreatedAt(TimeUtils.dateToString(user.getCreatedAt()));
        }
        if (user.getUpdatedAt() != null) {
            resp.setUpdatedAt(TimeUtils.dateToString(user.getUpdatedAt()));
        }
        return resp;
    }
}
